package com.aplink.dellcommercial.fragments;

import android.os.Bundle;
import android.os.Message;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.database.DellDataTable;
import com.aplink.dellcommercial.webservices.DataResult;
import com.aplink.dellcommercial.webservices.WebServiceHelper;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.constant.HandlerConstant;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.handler.IHandleMessage;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.ControlSpec;

public class TipDetailFragment extends GenericFragment implements
        IHandleMessage {
    private GenericObject mTipDisplay = null;

    @Override
    public int getFormDesign() {
        return R.raw.form_design_tip_detail;
    }

    @Override
    public int getContentData() {
        return R.raw.content_data_tip_detail;
    }

    @Override
    public String getTable() {
        return DellDataTable.TipDetail.TABLE_NAME;
    }

    @Override
    protected void onHandleControlClickEvent( ControlSpec controlSpec ) {
        String validationGroup = controlSpec.getValidationGroup();
        if ( validationGroup.equalsIgnoreCase( "close" ) ) {
            finishActivity();
        }
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState ) {
        super.onActivityCreated( savedInstanceState );
        mTipDisplay = new GenericObject();
        this.getHandler().setHandleMessage( this );
        Bundle bundle = getActivity().getIntent().getExtras();
        GenericObject genericObject = (GenericObject) bundle
                .getSerializable( ConfigParameter.GENERIC_OBJECT );
        if ( genericObject != null ) {
            getTipDetail( genericObject );
        }

    }

    private void getTipDetail( GenericObject genericObject ) {
        boolean isExists = selectCommand().isExists( genericObject,
                getObjPropCol() );
        mTipDisplay.addAttributesFromAnotherGenericObject( genericObject );
        String tipID = genericObject.getKeyAttribute( getObjPropList() ).get( 0 )
                .getValue().toString();
        if ( isExists ) {
            String whereClause = SqlMethods.getInstance().equal( SqlDataType.INTEGER,
                    DellDataTable.TipDetail.TP_ID, tipID ).toString();
            GenericObject tipDetail = selectCommand().get( getObjPropCol(),
                    whereClause );
            mTipDisplay.addAttributesFromAnotherGenericObject( tipDetail );
            showTip();
        } else {

            WebServiceHelper.getInstance().getTipDetail( getActivity(),
                    getHandler(), tipID, getObjPropList() );
        }
    }

    @Override
    public int getContentView() {
        return R.layout.aplink_dellcommercial_tip_detail;
    }

    private boolean saveTipDetail( GenericObject tipDetail ) {
        return insertCommand().insertOrUpdate( tipDetail, getObjPropCol() ) != - 1;
    }

    private void showTip() {
        this.setPassedGenericObject( mTipDisplay );
        getHandler().sendEmptyMessage( RELOAD_DATA );
    }

    @Override
    public void onHandleMessage( Message msg ) {
        if ( msg.what == HandlerConstant.HTTP_TASK_RESULT ) {
            DataResult dataResult = (DataResult) msg.obj;
            GenericObject tipDetail = (GenericObject) dataResult.getData();
            if ( saveTipDetail( tipDetail ) ) {
                mTipDisplay.addAttributesFromAnotherGenericObject( tipDetail );
                showTip();
            }
        } else if ( msg.what == RELOAD_DATA ) {
            this.initData();
            this.loadDataToForm();
        }
    }

}
